﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using MethodWorx.CMS.TemplateParser.Installer;
using MethodWorx.CMS.Membership;

namespace MethodWorx.CMS.UI.Areas.Dashboard.Models
{
    public class TemplateGalleryModel
    {
        private string[] files;
        private User user;

        public TemplateGalleryModel(string [] files,bool dialogMode, User user)
        {
            this.user = user;
            this.files = files;
            this.DialogMode = dialogMode;
        }

        public bool DialogMode { get; private set; }

        public SelectList GetCategorySelectList()
        {
            List<string> categories = new List<string>();
            //
            //  ok, lets look at the files
            foreach(string file in files)
            {
                using(Stream stream = File.OpenRead(file))
                {
                    var engine = new InstallerEngine(file, stream, user);
                    categories.Add(engine.GetParser().InstallerType.ToString());
                }
            }

            return new SelectList(categories.Distinct());
        }

        internal IEnumerable<InstallerEngine> GetInstallers()
        {
            //
            //  ok, lets look at the files
            foreach (string file in files)
            {
                using (Stream stream = File.OpenRead(file))
                {
                    yield return new InstallerEngine(file, stream, this.user);
                    
                }
            }
        }



        internal void UpdateFromFormCollection(FormCollection form)
        {
            throw new NotImplementedException();
        }
    }
}